<template>
	
	<view :style="{paddingTop:shh+'px'}" class=" posifi  zindex5">
		<view @click="back" class="flex-ju-c posifi wh70-70 mt30 ra100  zindex5">
			<back></back>
		</view>
	</view>

	<view style="height: 100vh;position: absolute;width: 100vw;">
		<image mode="widthFix" class="image" style="position: absolute;top: 0;" src="/static/choubg.png"></image>
	</view>
	
	<gb-choujiang :AwardList="AwardList" :turndata="turndata" :sjNum="sjNum" @updateMoney="updateMoney"></gb-choujiang>

	<uni-popup mode="center" ref="isjiang">

		<view class="posire bgfff height600 width500 ra30">
			<view class="flex-ju-c ">
				<!-- <view v-if="moneys.length==1" class="colorff0200 bold size55 posiab zindex5 top135">{{moneys[0].git}}</view> -->
				<view v-if="moneys.length==1" class=" bold size55  zindex5 mt255">
					{{moneys[0].git}}
					<view style="">x1</view>
				</view>
				
				<view v-else class="flex-ju-c width600">
					<view  class="colorff0200 bold size24 posiab zindex5 top150 center width500 flex-a-i flex-wrap">
						<view v-for="(item,index) in arrmap" :key="index" class="flex-a-i mt90 borderf72825 ml17">
							<view class="one" style="width:50px;">
								<text class="" >
								{{index}}</text>
							</view>
							<text class="size26 mt20 ml5 mr10">X1 </text>
						</view>
					</view>
				</view>
				
			</view>
			<view class="flex-ju-c mt100">
				<view @click="$refs.isjiang.close()" class="wh430-100 colorfff flex-ju-c ra20 bg000 posiab ">
					确认
				</view>
			</view>
			<image mode="widthFix" class="image posiab top-250" src="/static/huode.png"></image>
		</view>

	</uni-popup>
	
</template>

<script>
	export default {
		data() {
			return {
				arrmap:[],
				turndata: {
					git: {}
				},
				git: {
					info: {
						material: [],
						prize: []
					}
				},
				moneys: {
					prize: {}
				},
				// 中奖的索引，也就是AwardList数组的索引，sjNum不能等于4，4是立即抽奖
				sjNum: 7,
				// false前端，true前端
				isQd: false,
				// 可自己后端获取，也可以后端  percentage为概率 10代表10%   100代表100%   注意：必须要有一个概率为100%，
				AwardList: []
			}
		},
		onLoad() {
			//this.getData()
			this.getg()
		},
		mounted() {

 // this.$refs.isjiang.open()
 
			// var countDuplicates = (arr, key) => {
			//   return arr.reduce((acc, obj) => {
			//     const keyValue = obj[key];
			//     acc[keyValue] = (acc[keyValue] || 0) + 1;
			//     return acc;
			//   }, {});
			// };
			
			// const counts = countDuplicates(money, 'git');
			
			// this.arrmap=counts
			
		},
		methods: {
			tapchiou() {
				uni.navigateTo({
					url: "jilu"
				})
			},
			getg() {
				this.isload = true;
				this.http("turntable").then(res => {
					this.turndata = res.data
					var arr = []
					res.data.git.map((item, index) => {
						if (index == 4) {
							arr.push({
								git: "荷花1",
								id: '-1',
								img: "/static/dianji.png"
							})
						}
						arr.push(item)
					})
					console.log(arr)
					this.AwardList = arr
					uni.setStorageSync('jiang', res.data.info)
				}).catch(res => {})
			},
			tapright() {
				uni.navigateTo({
					url: "jilu"
				})
			},
			updateMoney(money) {
				// //console.log()
				// uni.showToast({
				// 	title: '恭喜获得：' + this.AwardList[money].name,
				// 	icon: "none",
				// 	position: 'bottom'
				// })

				//console.log(money)
				this.moneys = money
				
				console.log(money,"ggg")

				var countDuplicates = (arr, key) => {
				  return arr.reduce((acc, obj) => {
				    const keyValue = obj[key];
				    acc[keyValue] = (acc[keyValue] || 0) + 1;
				    return acc;
				  }, {});
				};
				
				
				const counts = countDuplicates(money, 'git');
				this.arrmap=counts
					
					console.log(this.moneys.length,"hhh")


				this.$refs.isjiang.open()

				this.getg()

				// 中奖后获取最新状态
				// this.getData()
			},
			// 获取本次中奖的
			getData() {
				// 概率指定，如果是前端控制，如果后端控制下面步骤判断步骤可以省略  直接后端获取sjNum值即可
				if (this.isQd) {
					// ------------------------------这里写获取后端中奖索引
					// 后端赋值
					this.sjNum = this.sjNum || 0

				} else {

					// 概率控制  概率100%
					let sj = Math.round(Math.random() * 99 + 1)
					// 前端控制概率
					let arr = []
					//console.log("sj: ", sj);

					for (let i = 0; i < this.AwardList.length; i++) {
						if (this.AwardList[i].percentage >= sj) {
							arr.push(this.AwardList[i])
						}
					}
					//console.log("arr: ", arr);
					// 随机获得索引
					if (arr.length > 1) {
						this.sjNum = arr[Math.round(Math.random() * arr.length)].index_num
					} else {
						this.sjNum = arr[0].index_num
					}

					//console.log(this.sjNum);
				}
			}
		}
	}
</script>

<style scoped>
	.chouqjaing {
		position: absolute;
		right: 0;
	}

	.xingg {
		height: 40px;
	}

	.thumbchou {
		width: 60px;
		height: 60px;
	}

	.guanbi {
		width: 80rpx;
		height: 50rpx;
		position: absolute;
		bottom: 0;
	}

	.textvd {
		width: 460rpx;
		font-size: 70rpx;
		margin: auto;
		position: absolute;
		margin-top: 370rpx;
	}

	.jiangimg {
		width: 230rpx;
		height: 230rpx;
		margin-top: 40rpx;
	}

	.qued {
		width: 300rpx;
		height: 68rpx;
		position: absolute;
		bottom: 120rpx;
		color: #fff;
	}

	.huode {
		margin-top: 100rpx;
		width: 299.39rpx;
	}

	.bgyan {
		width: 500rpx;
		background: #1C1C1C;
		border-radius: 7rpx;
	}
	.one{overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-box-align: start;-webkit-box-orient: vertical;-webkit-line-clamp:1;}
</style>